package com.wtwd.campus.hxy.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.wtwd.campus.hxy.constant.RespCode;
import com.wtwd.campus.hxy.model.vo.ResultVO;
import com.wtwd.campus.hxy.service.HxyPhoneService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author ldaoliang
 * @Date create in 16:49 2021/8/12
 * @Description
 */
@RestController
@RequestMapping("/hxy/phone")
public class HxyDevicePhoneController {

    @Autowired
    private HxyPhoneService hxyPhoneService;

    /**
     * 批量设置白名单
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/setWhiteList")
    public Object setWhiteList(@RequestBody JSONObject jsonObject) {
        String imei = jsonObject.getString("imei");
        List<Map> numberList = jsonObject.getJSONArray("numberList").toJavaList(Map.class);
        return hxyPhoneService.setAllWhite(numberList, imei);
    }

    /**
     * 亲情号码设置接⼝
     *
     * @param
     * @return
     */
    @PostMapping("/setFamilyPhone")
    public Object setFamilyPhone(@RequestBody JSONObject jsonObject) {
        String imei = jsonObject.getString("imei");
        List<Map> numberList = jsonObject.getJSONArray("familyNumberList").toJavaList(Map.class);
        return hxyPhoneService.setFamilyPhone(imei, numberList);
    }

    /**
     * SB需求  SB带参
     * 设置SOS号码
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/setSOSPhone")
    public Object setSOSPhone(@RequestBody JSONObject jsonObject) {
        String imei = jsonObject.getString("imei");
        JSONArray jsonArray = jsonObject.getJSONArray("sosNumberList");
        if (jsonArray.size() > 3) {
            return ResultVO.error(RespCode.SOS_PHONE_MAXED);
        }
        List<String> sosList = new ArrayList<>();
        jsonArray.stream().forEach(x -> {
            JSONObject json = (JSONObject) x;
            sosList.add(json.getString("sosNumber"));
        });
        return hxyPhoneService.setSOSPhone(imei,sosList);
    }

    /**
     * 查询亲情号码和SOS号码
     * @param jsonObject
     * @return
     */
    @PostMapping("/queryFamilyAndSosPhone")
    public ResultVO queryFamilyAndSOSPhone(@RequestBody JSONObject jsonObject) {
        String imei = jsonObject.getString("imei");
        return hxyPhoneService.getFamilyPhoneAndSOSPhone(imei);
    }

    /**
     * 查询白名单列表
     * @param jsonObject
     * @return
     */
    @PostMapping("/queryWhiteCallList")
    public ResultVO queryWhiteCall(@RequestBody JSONObject jsonObject) {
        String imei = jsonObject.getString("imei");
        return hxyPhoneService.getWhiteCallList(imei);
    }

    /**
     * 查询sim卡号  TODO 不够稳定，待设备端优化
     * @param jsonObject
     * @return
     */
    @PostMapping("/querySimCard")
    public Object querySIMCard(@RequestBody JSONObject jsonObject) {
        String imei = jsonObject.getString("imei");
        return hxyPhoneService.getDeviceSimCard(imei);
    }


}
